Imports System
Imports System.Data
Imports System.Collections.Generic

Imports Framework


'Table-Row Class (Customisable half)
Partial Public Class CUserRole

#Region "Constants"
    Private Shared JOIN_USER As String = String.Concat(TABLE_NAME, " LEFT OUTER JOIN ", CUser.TABLE_NAME, " ON URUserLogin=UserLoginName")
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(uRUserLogin As String, uRRoleName As String)
        MyBase.New(uRUserLogin, uRRoleName)
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc As CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    Public Sub New(ByVal dataSrc As CDataSrc, uRUserLogin As String, uRRoleName As String)
        MyBase.New(dataSrc, uRUserLogin, uRRoleName)
    End Sub

    'Transactional (shares an open connection)
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal uRUserLogin As String, uRRoleName As String, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, uRUserLogin, uRRoleName, txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
        'Custom default values (e.g. DateCreated column)
        
        'Member variables (e.g. for child collections)
        
    End Sub
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Members"
    'Foreign Keys   *Consider using <NonSerialized()> attribute
    <NonSerialized()> Private m_user As CUser

    'Child Collections  *Consider using <NonSerialized()> attribute

    'Xml Data (as high-level objects)

#End Region

#Region "Properties - Relationships"    
    'Relationships - Foriegn Keys (e.g parent)
    Public ReadOnly Property [Role]() As CRole
        Get
            Return CRole.Cache.GetById(Me.URRoleName)
        End Get
    End Property
    Public Property [User]() As CUser
        Get
            If IsNothing(m_user) Then
                SyncLock (Me)
                    If IsNothing(m_user) Then
                        m_user = New CUser(Me.URUserLogin)
                    End If
                End SyncLock
            End If
            Return m_user
        End Get
        Set(ByVal Value As CUser)
            m_user = Value
        End Set
    End Property

    'Relationships - Collections (e.g. children)

#End Region

#Region "Properties - Customisation"
    Public Overrides Sub Save(ByVal txOrNull As System.Data.IDbTransaction)
        MyBase.Save(txOrNull)

        If CUser.IsLoggedIn AndAlso CUser.CurrentLogin = URUserLogin Then CUser.Current.UserRoles = Nothing
    End Sub
    Public Overrides Sub Delete(ByVal txOrNull As System.Data.IDbTransaction)
        MyBase.Delete(txOrNull)

        If CUser.IsLoggedIn AndAlso CUser.CurrentLogin = URUserLogin Then CUser.Current.UserRoles = Nothing
    End Sub
#End Region

#Region "Save/Delete Overrides"
     'Can Override MyBase.Save/Delete (e.g. Cascade deletes, or insert related records)
#End Region

#Region "Cloning"
    Public Function Clone(target as CDataSrc, txOrNull as IDbTransaction) As CUserRole ', parentId As Integer) As CUserRole
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CUserRole(Me, target)
        copy.Save(txOrNull)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.URUserLogin)

        Return copy
    End Function
#End Region

#Region "Custom Database Queries"
    'Dynamic sql: Use SelectWhere, SelectCount, SelectSum, SelectDistinct
    'Stored procedures: Use MakeList (cast result as CUserRoleList)
    'See also the auto-generated examples (need to identify FK columns during code gen.)
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
